/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package streams.examples.streams;

import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.streams.Topology;
import org.apache.kafka.streams.kstream.KStream;
import org.apache.kafka.streams.kstream.ValueMapper;

import java.util.Arrays;
import java.util.Properties;
import java.util.concurrent.CountDownLatch;

/**
 * 在本示例中，我们使用高级 Streams DSL 实现了一个简单的 LineSplit 程序
 * 该程序从源主题 "streams-plaintext-input" 读取数据，其中消息的值表示文本行；
 * 代码将每行文本字符串拆分为单词，然后将结果写回到一个名为 "streams-linesplit-output" 的目标主题，其中每条记录代表一个单独的单词。
 */
public class LineSplit {

    /**
     * 主函数用于启动一个Kafka Streams应用程序
     * 该程序读取来自Kafka主题的文本消息，将每条消息拆分成单词，并将结果写入另一个Kafka主题
     *
     * @param args 命令行参数
     */
    public static void main(String[] args) {
        // 初始化Kafka Streams配置
        Properties props = new Properties();
        props.put(StreamsConfig.APPLICATION_ID_CONFIG, "streams-linesplit");
        props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.0.228:9092");
        props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass());
        props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass());

        // 创建StreamsBuilder对象，用于构建Kafka Streams拓扑
        final StreamsBuilder builder = new StreamsBuilder();

        // 从Kafka主题读取消息，并将每条消息的值拆分成单词
        KStream<String, String> source = builder.stream("streams-plaintext-input");
        KStream<String, String> words = source.flatMapValues(value -> Arrays.asList(value.split("\\W+")));

        // 将拆分后的单词写入另一个Kafka主题
        builder.<String, String>stream("streams-plaintext-input")
                .flatMapValues(value -> Arrays.asList(value.split("\\W+")))
                .to("streams-linesplit-output");

        // 构建Kafka Streams拓扑
        final Topology topology = builder.build();
        // System.out.println(topology.describe());

        // 创建KafkaStreams对象并启动应用程序
        final KafkaStreams streams = new KafkaStreams(topology, props);
        final CountDownLatch latch = new CountDownLatch(1);

        // 添加关闭钩子，以便在按下control-c时优雅地关闭应用程序
        Runtime.getRuntime().addShutdownHook(new Thread("streams-shutdown-hook") {
            @Override
            public void run() {
                streams.close();
                latch.countDown();
            }
        });

        // 启动Kafka Streams应用程序并等待其终止
        try {
            streams.start();
            latch.await();
        } catch (Throwable e) {
            System.exit(1);
        }
        System.exit(0);
    }

}
